package com.atguigu.gulimall.order.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 监听某个queue
 */
@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 监听消息-实体
     *
     * @param msg    消息
     * @param entity 消息体
     */
    @RabbitHandler
    void recieveMsgFromObj(Message msg, OrderEntity entity, Channel channel) {
        System.out.println("Msg:" + msg + "===>" + entity);
        MessageProperties messageProperties = msg.getMessageProperties();

        try {
            channel.basicAck(messageProperties.getDeliveryTag(), false);
//            channel.basicNack(messageProperties.getDeliveryTag(), false,false);
        } catch (Exception ignore) {
        }
    }

    /**
     * 监听消息-字符串
     *
     * @param msg     消息
     * @param msgBody 字符串
     */
    @RabbitHandler
    void recieveMsgFromStr(Message msg, String msgBody) {
        System.out.println("Msg:" + msg + "===>" + msgBody);
    }

}